package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.DeliverRecordDao;
import cn.gson.crm.model.mapper.DeliverRecordMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class DeliverRecordService {
    @Autowired
    DeliverRecordMapper deliverRecordMapper;
    @Autowired
    DeliverRecordDao deliverRecordDao;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询当前登录人所有交付记录
    public Map<String,Object> selectrec(HttpSession session,Integer pid,Integer page, Integer size){
        pid=fouyuan(session);
        List<DeliveryRecord> deliveryPlans2=deliverRecordMapper.selectrec(pid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<DeliveryRecord> deliveryPlans=deliverRecordMapper.selectrec(pid);
        User login = (User)session.getAttribute("login");
        int ygId= (int) login.getEmployees().get(0).getYgId();
        List<Customer>  customerList=deliverRecordMapper.selectcustomer(ygId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",deliveryPlans);
        map.put("customerList",customerList);
        map.put("tong",deliveryPlans2);
        return map;
    };
    //客户
    public List<Customer> selectcustomer(Integer yg){
        return deliverRecordMapper.selectcustomer(yg);
    };
    //通过客户id查询交付计划
    public List<DeliveryPlan> selectdel(Integer cid){
        return  deliverRecordMapper.selectdel(cid);

    };
    //多条件查询
    public  Map<String,Object> selectwhere( HttpSession session,Integer pid, String where,Integer page, Integer size){
        pid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<DeliveryRecord>  deliveryPlans=deliverRecordMapper.selectwhere(pid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",deliveryPlans);
        return map;
    };
    //时间查询
    public Map<String,Object> selecttime(HttpSession session, Integer pid,String statr,String endtime,Integer page, Integer size){
        pid=fouyuan(session);
       Page<Object> obj = PageHelper.startPage(page, size);
        List<DeliveryRecord>  deliveryPlans=deliverRecordMapper.selecttime(pid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",deliveryPlans);
        return map;
    };
    //查询订单
    public List<DeliveryPlan> selectorder(Integer pid){
        return deliverRecordMapper.selectorder(pid);
    };
    //新增
    public void savedelive(DeliveryRecord deliveryRecord){

        deliverRecordDao.save(deliveryRecord);
        deliverRecordMapper.updatedel((int) deliveryRecord.getDeliveryPlan().getPlanId());
    }
    //查询详单
    public List<OrderDetails> selectoid(Integer oid){
        return deliverRecordMapper.selectoid(oid);
    }
    public List<DeliveryScheduleDetails> selectDe(Integer did){
        return deliverRecordMapper.selectDe(did);
    };
}
